package com.kingsalert.kingsalert.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;

import com.kingsalert.kingsalert.R;
import com.kingsalert.kingsalert.bean.userinfo.BankAccount_Fr;
import com.kingsalert.kingsalert.util.http.HttpUtil;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class DeleteAccountDialog_Fr extends Dialog {

    private final FragmentActivity activity;

    private final BankAccount_Fr account;

    private final String page;

    private final Runnable dltActRespRunnable;

    public DeleteAccountDialog_Fr(@NonNull FragmentActivity activity, BankAccount_Fr account,String page,Runnable dltActRespRunnable) {
        super(activity);
        this.activity = activity;
        this.account = account;
        this.page = page;
        this.dltActRespRunnable = dltActRespRunnable;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_delete_account_fr);

        getWindow().setBackgroundDrawableResource(R.color.transparent);
        setCanceledOnTouchOutside(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        initView();
    }

    @SuppressLint("SetTextI18n")
    private void initView(){

        TextView accountmobileTv  = findViewById(R.id.ddaf_tv_accountmobile);
        ImageView accountlogoIv = findViewById(R.id.ddaf_iv_logo);
        TextView accountchannelTv = findViewById(R.id.ddaf_tv_accountchannel);

        //取消
        findViewById(R.id.ddaf_btn_cancel).setOnClickListener(v -> {
            HttpUtil.getInstance().saveBuriedPoint(page,"_DELETE_CANCEL_CLICK",account.getWalletMobile());
            dismiss();
        });

        //提交
        findViewById(R.id.ddaf_btn_submit).setOnClickListener(v -> {
            HttpUtil.getInstance().saveBuriedPoint(page,"_DELETE_CONFIRM_CLICK",account.getWalletMobile());
            HttpUtil.getInstance().deleteAccount_Fr(activity,account.getId(),() -> {
                if (dltActRespRunnable!=null) dltActRespRunnable.run();
            });
            dismiss();
        });

        if (account != null){

            accountmobileTv.setText(account.getWalletMobile());
            accountchannelTv.setText(account.getWalletDesc());

            //加载图标
            if (account.getBitmap()==null) {

                new Thread(() -> {

                    try {

                        String urlStr = account.getWalletLogo();
                        if (TextUtils.isEmpty(urlStr)) return;

                        HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(urlStr).openConnection();

                        httpURLConnection.setConnectTimeout(5000);
                        httpURLConnection.setRequestMethod("GET");
                        if (httpURLConnection.getResponseCode()==200){
                            InputStream inputStream = httpURLConnection.getInputStream();
                            Bitmap logoBm = BitmapFactory.decodeStream(inputStream);
                            account.setBitmap(logoBm);
                            new Handler(Looper.getMainLooper()).post(() -> accountlogoIv.setImageBitmap(logoBm));
                        }

                    }catch (IOException e){
                        e.printStackTrace();
                    }

                }).start();

            } else accountlogoIv.setImageBitmap(account.getBitmap());

        }

    }

}
